﻿using Abp.Dependency;
using Abp.IO.Extensions;
using Abp.Reflection.Extensions;
using Mt.Site.Core.Url;
using System;
using System.Collections.Concurrent;
using System.Text;

namespace Mt.Site.Core.Net.Emailing
{
    public class EmailTemplateProvider : IEmailTemplateProvider, ISingletonDependency
    {
        private readonly IWebUrlService _webUrlService;
        private readonly ConcurrentDictionary<string, string> _defaultTemplates;

        public EmailTemplateProvider(IWebUrlService webUrlService)
        {
            _webUrlService = webUrlService;
            _defaultTemplates = new ConcurrentDictionary<string, string>();
        }

        public string GetDefaultTemplate(int? tenantId)
        {
            return _defaultTemplates.GetOrAdd(EmailTemplateName.Default, key =>
            {
                using (var stream = typeof(EmailTemplateProvider).GetAssembly()
                    .GetManifestResourceStream("Mt.Site.Core.Net.Emailing.EmailTemplates.default.html"))
                {
                    var bytes = stream.GetAllBytes();
                    var template = Encoding.UTF8.GetString(bytes, 3, bytes.Length - 3);
                    template = template.Replace("{THIS_YEAR}", DateTime.Now.Year.ToString());
                    return template;
                }
            });
        }

        public string GetForgetPasswordTemplate()
        {
            return _defaultTemplates.GetOrAdd(EmailTemplateName.FrontEndForgetPassword, key =>
            {
                using (var stream = typeof(EmailTemplateProvider).GetAssembly()
                .GetManifestResourceStream("Mt.Site.Core.Net.Emailing.EmailTemplates.E-forgetpassword.html"))
                {
                    var bytes = stream.GetAllBytes();
                    return Encoding.UTF8.GetString(bytes, 3, bytes.Length - 3);
                }
            });
        }

        public string GetVerificationTemplate()
        {
            return _defaultTemplates.GetOrAdd(EmailTemplateName.FrontEndAccountVerification, key =>
            {
                using (var stream = typeof(EmailTemplateProvider).GetAssembly()
                .GetManifestResourceStream("Mt.Site.Core.Net.Emailing.EmailTemplates.Verification.html"))
            {
                var bytes = stream.GetAllBytes();
                return Encoding.UTF8.GetString(bytes, 3, bytes.Length - 3);
                }
            });
        }
        public string GetAccountCreatedTemplate()
        {
            return _defaultTemplates.GetOrAdd(EmailTemplateName.FrontEndAccountCreated, key =>
            {
                using (var stream = typeof(EmailTemplateProvider).GetAssembly()
            .GetManifestResourceStream("Mt.Site.Core.Net.Emailing.EmailTemplates.E-accountcreated.html"))
                {
                    var bytes = stream.GetAllBytes();
                    return Encoding.UTF8.GetString(bytes, 3, bytes.Length - 3);
                }
            });
        }

        public string GetActivationTemplate()
        {
            return _defaultTemplates.GetOrAdd(EmailTemplateName.FrontEndAccountActivation, key =>
            {
                using (var stream = typeof(EmailTemplateProvider).GetAssembly()
        .GetManifestResourceStream("Mt.Site.Core.Net.Emailing.EmailTemplates.E-activation.html"))
                {
                    var bytes = stream.GetAllBytes();
                    return Encoding.UTF8.GetString(bytes, 3, bytes.Length - 3);
                }
            });
        }

        public string GetEmailChangeTemplate()
        {
            return _defaultTemplates.GetOrAdd(EmailTemplateName.FrontEndEmailChange, key =>
            {
                using (var stream = typeof(EmailTemplateProvider).GetAssembly()
    .GetManifestResourceStream("Mt.Site.Core.Net.Emailing.EmailTemplates.E-emailchange.html"))
                {
                    var bytes = stream.GetAllBytes();
                    return Encoding.UTF8.GetString(bytes, 3, bytes.Length - 3);
                }
            });
        }
    }
}